package com.uam.web.controller;

import com.uam.common.annotation.Log;
import com.uam.common.exception.UamException;
import com.uam.service.AircraftService;
import com.uam.web.common.model.BusinessType;
import com.uam.web.common.model.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author uam
 * @create 2023/12/1 0:55
 * @email uam@uam.com
 */
@Api(tags = "飞行器管理")
@RestController
@RequestMapping("/aircraft")
public class AircraftController {
    @Autowired
    private AircraftService aircraftService;

    @Log(title = "添加无人机")
    @ApiOperation(value = "添加飞行器")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户登录token", paramType = "header", required = true),
            @ApiImplicitParam(name = "airplaneId", value = "不需要传,自动生成"),
            @ApiImplicitParam(name = "airplaneName", value = "飞行器名称"),
            @ApiImplicitParam(name = "maxEndurance", value = "最大续航"),
            @ApiImplicitParam(name = "minAltitude", value = "最小高度"),
            @ApiImplicitParam(name = "maxAltitude", value = "最大高度"),
            @ApiImplicitParam(name = "maxSpeed", value = "最大速度"),
            @ApiImplicitParam(name = "avgSpeed", value = "平均速度"),
            @ApiImplicitParam(name = "modelName", value = "模型名称"),
            @ApiImplicitParam(name = "flightInterval", value = "飞行间隔"),
            @ApiImplicitParam(name = "verticalAcceleration", value = "垂直加速度"),
            @ApiImplicitParam(name = "horizontalAcceleration", value = "水平加速度"),
    })
    @PostMapping("/add")
    @ResponseBody
    public Result add(HttpServletRequest request,
                      @RequestParam(required = false) Long airplaneId,
                      @RequestParam(required = true) String airplaneName,
                      @RequestParam(required = true) Integer maxEndurance,
                      @RequestParam(required = true) Integer minAltitude,
                      @RequestParam(required = true) Integer maxAltitude,
                      @RequestParam(required = true) Integer maxSpeed,
                      @RequestParam(required = true) Integer avgSpeed,
                      @RequestParam(required = true) String modelName,
                      @RequestParam(required = true) Integer flightInterval,
                      @RequestParam(required = false) Integer verticalAcceleration,
                      @RequestParam(required = false) Integer horizontalAcceleration

    ) {
        checkHeightAdd(minAltitude,maxAltitude,avgSpeed,maxEndurance,flightInterval);
        return aircraftService.add(request,
                airplaneId,
                airplaneName,
                maxEndurance,
                minAltitude,
                maxAltitude,
                maxSpeed,
                avgSpeed,
                modelName,
                flightInterval,
                verticalAcceleration,
                horizontalAcceleration);
    }


    private void checkHeightAdd(Integer minAltitude,Integer maxAltitude,Integer avgSpeed,Integer maxEndurance,Integer flightInterval){
        if(minAltitude == null || minAltitude<=0){
            throw  new UamException("最低飞行高度必须大于0米！");
        }

        if(maxAltitude == null || maxAltitude<=40){
            throw  new UamException("最高飞行高度必须大于等于40米！");
        }

        if(minAltitude>maxAltitude){
            throw  new UamException("最低飞行高度必须小于最高飞行高度！");
        }

        if(avgSpeed == null || avgSpeed<=0){
            throw  new UamException("平均速度不能为空，且必须大于0！");
        }

        if(maxEndurance == null || maxEndurance<=0){
            throw  new UamException("续航不能为空，且必须大于0！");
        }

        if(flightInterval == null || flightInterval<=0){
            throw  new UamException("安全距离不能为空，且必须大于0！");
        }
    }



    @Log(title = "编辑无人机")
    @ApiOperation(value = "编辑飞行器")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户登录token", paramType = "header", required = true),
            @ApiImplicitParam(name = "airplaneId", value = "不需要传,自动生成"),
            @ApiImplicitParam(name = "airplaneName", value = "飞行器名称"),
            @ApiImplicitParam(name = "maxEndurance", value = "最大续航"),
            @ApiImplicitParam(name = "minAltitude", value = "最小高度"),
            @ApiImplicitParam(name = "maxAltitude", value = "最大高度"),
            @ApiImplicitParam(name = "maxSpeed", value = "最大速度"),
            @ApiImplicitParam(name = "avgSpeed", value = "平均速度"),
            @ApiImplicitParam(name = "modelName", value = "模型名称"),
            @ApiImplicitParam(name = "flightInterval", value = "飞行间隔"),
            @ApiImplicitParam(name = "verticalAcceleration", value = "垂直加速度"),
            @ApiImplicitParam(name = "horizontalAcceleration", value = "水平加速度"),
    })
    @PostMapping("/edit")
    @ResponseBody
    public Result edit(HttpServletRequest request,
                       @RequestParam(required = true) Long airplaneId,
                       @RequestParam(required = false) String airplaneName,
                       @RequestParam(required = false) Integer maxEndurance,
                       @RequestParam(required = false) Integer minAltitude,
                       @RequestParam(required = false) Integer maxAltitude,
                       @RequestParam(required = false) Integer maxSpeed,
                       @RequestParam(required = false) Integer avgSpeed,
                       @RequestParam(required = false) String modelName,
                       @RequestParam(required = false) Integer flightInterval,
                       @RequestParam(required = false) Integer verticalAcceleration,
                       @RequestParam(required = false) Integer horizontalAcceleration

    ) {
        checkHeightEdit(minAltitude,maxAltitude);
        return aircraftService.edit(request,
                airplaneId,
                airplaneName,
                maxEndurance,
                minAltitude,
                maxAltitude,
                maxSpeed,
                avgSpeed,
                modelName,
                flightInterval,
                verticalAcceleration,
                horizontalAcceleration);
    }


    private void checkHeightEdit(Integer minAltitude,Integer maxAltitude){
        if(minAltitude != null && minAltitude<=0){
            throw  new UamException("最低飞行高度必须大于0！");
        }

        if(maxAltitude != null && maxAltitude<=40){
            throw  new UamException("最高飞行高度必须大于等于40！");
        }

    }

    @Log(title = "删除无人机")
    @ApiOperation(value = "删除飞行器")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户登录token", paramType = "header"),
            @ApiImplicitParam(name = "airplaneIds", value = "飞行器id", dataTypeClass = List.class),
    })
    @PostMapping("/delete")
    @ResponseBody
    public Result delete(HttpServletRequest request,
                         @RequestParam(required = true) List<Long> airplaneIds
    ) {
        if(airplaneIds == null ||  airplaneIds.size()==0){
            throw  new UamException("删除的飞行器的id必传！");
        }
        return aircraftService.delete(request, airplaneIds);
    }

    @Log(title = "查看无人机列表", businessType = BusinessType.QUERY_LOG)
    @ApiOperation(value = "飞行器列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户登录token", paramType = "header"),
            @ApiImplicitParam(name = "pageNum", value = "当前页", dataType = "Integer", defaultValue = "0", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "分页条数", dataType = "Integer", defaultValue = "10", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "airplaneName", value = "飞行器名称，不按此条件查询可不传", dataTypeClass = String.class),
            @ApiImplicitParam(name = "createBy", value = "创建人，不按此条件查询可不传", dataTypeClass = String.class),
            @ApiImplicitParam(name = "airplaneId", value = "飞行器id,不按此条件查询传-1", dataTypeClass = Long.class)

    })
    @PostMapping("/list")
    @ResponseBody
    public Result list(@RequestParam(required = false, defaultValue = "0") Integer pageNum,
                       @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                       @RequestParam(required = false) String airplaneName,
                       @RequestParam(required = false) String createBy,
                       @RequestParam(required = true) Integer airplaneId) {
        return aircraftService.list(pageNum, pageSize, airplaneName, createBy,airplaneId);

    }

}
